<?php
/**
 * Query.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/11/17- 10:43 上午
 */
namespace common\models\es\dataProvider\tbMallProductsSkus;

use common\helpers\AmountHelper;
use common\models\es\EsPddMallProductsSkus;

class Query
{
    /**
     * 查询字段
     * @var array
     */
    protected $fields = [];

    /**
     * 查询条件
     * @var array
     */
    protected $query = [];

    /**
     * 排序方式
     * @var string
     */
    protected $sort = [];


    /**
     * 每页条数
     * @var
     */
    protected $size;


    /**
     * 偏移量
     * @var
     */
    protected $from;


    public function __construct(array $params, array $fields = [], int $page = 1, int $size = 20)
    {
        $page = $page ? $page : 1;
        $size = $size ? $size : 20;

        $offset = max(($page - 1), 0) * $size;

        $this->from = $offset;
        $this->size = $size;
        if(isset($params['sort']) && is_array($params['sort']))
        {
            $this->sort = $params['sort'];
        }else{
            $this->sort = ['goods_id' => ['order' => 'DESC']];
        }


        $this->formatFields($fields);

        $this->formatQuery($params);
    }

    /**
     * 查询字段
     * @param $fields
     */
    protected function formatFields($fields)
    {
        $es = new EsPddMallProductsSkus();
        $attributes = $es->attributes();
        $search_fields = [];
        foreach ($fields as $field) {
            if (in_array($field, $attributes)) {
                $search_fields[$field] = $field;
            }
        }
        $this->fields = $search_fields ? array_values($search_fields) : $attributes;
    }

    /**
     * 查询条件
     * @param $params
     */
    protected function formatQuery($params)
    {
        $query = [];

        if (isset($params['goods_ids']) && !empty($params['goods_ids'])) {
            $query['goods_ids'] = is_array($params['goods_ids']) ? $params['goods_ids'] : [$params['goods_ids']];
        }

        if (isset($params['uid']) && !empty($params['uid'])) {
            $query['uid'] = intval($params['uid']);
        }

        if (isset($params['mall_id']) && !empty($params['mall_id'])) {
            $query['mall_id'] = $params['mall_id'];
        }


        if (isset($params['cat_id']) && is_array($params['cat_id'])) {
            $query['cat_id'] = $params['cat_id'];
        }


        if (isset($params['approve_status']) && !empty($params['approve_status'])) {
            $query['approve_status'] = intval($params['approve_status']);
        }

        //是否搬家
        if (isset($params['is_copy']) && $params['is_copy'] != null)
        {
            $query['is_copy'] = $params['is_copy'];
        }

        //商家编码
        if(isset($params['outer_id']) && !empty($params['outer_id']))
        {
            $query['outer_goods_id'] = explode(',',$params['outer_id']);
        }
        //运费模板
        if(isset($params['template_id']) && !empty($params['template_id']))
        {
            $query['cost_template_id'] = $params['template_id'];
        }

        //退换货
        if(isset($params['is_refundable']) && $params['is_refundable'] !=null)
        {
            $query['is_refundable'] = $params['is_refundable'];
        }

        //是否二手
        if(isset($params['second_hand']) && $params['second_hand'] != null)
        {
            $query['second_hand'] = $params['second_hand'];
        }

        //假一罚十
        if(isset($params['is_folt']) && $params['is_folt'] != null)
        {
            $query['is_folt'] = $params['is_folt'];
        }

        //是否预售
        if(isset($params['is_pre_sale']) && $params['is_pre_sale'] != null)
        {
            $query['is_pre_sale'] = $params['is_pre_sale'];
        }

        //商品类型
        if(isset($params['goods_type']) && $params['goods_type'] != null)
        {
            $query['goods_type'] = $params['goods_type'];
        }

        if (isset($params['goods_name']) && !empty($params['goods_name'])) {
            $query['goods_name'] = trim($params['goods_name']);
        }
        if (isset($params['words']) && !empty($params['words'])) {
            $query['words'] = $params['words'];
        }

        if (isset($params['min_price']) && !empty($params['min_price'])) {
            $query['min_price'] = AmountHelper::yuanTofen(trim($params['min_price']));
        }

        if (isset($params['max_price']) && !empty($params['max_price'])) {
            $query['max_price'] = AmountHelper::yuanTofen(trim($params['max_price']));
        }

        if (isset($params['start_num']) && !empty($params['start_num'])) {
            $query['start_num'] = intval($params['start_num']);
        }

        if (isset($params['end_num']) && !empty($params['end_num'])) {
            $query['end_num'] = intval($params['end_num']);
        }

        if (isset($params['approve_status_arr']) && !empty($params['approve_status_arr'])) {
            $query['approve_status_arr'] = $params['approve_status_arr'];
        }

        if (isset($params['isset_outer_id']) && !empty($params['isset_outer_id'])) {
            $query['isset_outer_id'] = $params['isset_outer_id'];
        }

        $this->query = $query;
    }



    /**
     * 获取检索的索引名称
     * @return string
     */
    protected function getIndexName(): string
    {
        return EsPddMallProductsSkus::INDEX;
    }
}
